package com.cy.pj.sys.service.impl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.pojo.Node;
import com.cy.pj.sys.dao.SysMenuDao;
import com.cy.pj.sys.pojo.SysMenu;
import com.cy.pj.sys.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 菜单业务管理1
 */

@Transactional(readOnly = false,
            rollbackFor = Throwable.class,
            isolation = Isolation.READ_COMMITTED)
@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    private SysMenuDao sysMenuDao;
    @Override
    public List<SysMenu> findMenus() {
        return sysMenuDao.selectMenus();
    }

    @Transactional(readOnly = true)
    @Override
    public SysMenu findById(Integer id) {
        return sysMenuDao.selectById(id);
    }


    @Override
    public int saveMenu(SysMenu entity) {
        return sysMenuDao.insertMenu(entity);
    }

    @Override
    public int updateMenu(SysMenu entity) {
        int i =sysMenuDao.updateMenu(entity);
        if (i==0)
            throw new ServiceException("修改的数据不存在");
        return i;
    }


    @Override
    public List<Node> findMenuTreeNodes() {
        //方式一
        return sysMenuDao.selectMenuTreeNodes();
        //方式2(拓展)
        //查找所有菜单信息:select * from sys_menus
        //List<SysMenu> menus = sysMenuDao.selectMenus();
        //在业务层对数据进行封装(一级菜单,二级菜单,三级菜单,....),递归算法
        //menus.stream().filter().collect()
    }
}


/**
 * 拓展模块:
 * 1.组织管理(部门管理)
 * 2.商品分类(一级分类,二级分类,..)
 * 3.文章分类(小说->武侠->...)
 */
